<?php

namespace App\Admin\Controllers;
/**
 * 此项目由百凯伦公司研发，后面省略一万字
 */
use App\Models\AfterSales;
use App\Models\Area;
use App\Util\Tool;
use App\Util\Url;
use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class AfterSalesController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '售后管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new AfterSales());
        $userid = auth()->user()->id ?? 0;
        $where = [];
        $where [] = ['suppliers_id', '=', $userid];//读取当前登录供应商的产品
        $grid->model()->where($where)->orderByDesc('id');
        $grid->fixColumns(0, 1);//固定列可滑动列表
        $grid->column('id', __('Id'));
        $grid->column('after_type', __('退货方式'))->display(function () {
            switch ($this->after_type) {
                case 1:
                    return '仅退款';
                case 2:
                    return '退货退款';
                case 3:
                    return '换货';
                default:
                    return '无';
            }
        });
        $grid->column('order_sn', __('订单号'));
        $grid->column('goods_status', __('货物状态'))->display(function () {
            switch ($this->goods_status) {
                case 1:
                    return '已收货';
                case 2:
                    return '未收货';
                default:
                    return '无';
            }
        });
        $grid->column('goods_num', __('商品数量'));
        $grid->column('back_reason', __('退款原因'));
        $grid->column('money', __('退款金额'));
        $grid->column('afterAddresses', __('收件地址'))->display(function () {
            $area = new Area();
            $province_name = $area->where('code', $this->address->province_id ?? 0)->value('name');
            $city_name = $area->where('code', $this->address->city_id ?? 0)->value('name');
            $district_name = $area->where('code', $this->address->district_id ?? 0)->value('name');
            $detail = $this->address->detail ?? '';
            if (!empty($province_name) && !empty($city_name) && !empty($district_name)) {
                return '收件人:' . $this->address->name . '电话:' . $this->address->phone . '详细地址:' . $province_name . '->' . $city_name . '->' . $district_name . '->' . $detail;
            }
        });

        $grid->column('images', __('上传凭证'))->display(function () {
            return $this->images;
        })->lightbox(['width' => 50, 'height' => 50]);

        $grid->column('describe', __('补充描述'));
        $grid->column('logis_company', __('物流公司'));
        $grid->column('logis_num', __('物流单号'));
        $grid->column('status', __('审核状态'))->display(function () {
            switch ($this->status) {
                case 1:
                    return '审核通过';
                case 2:
                    return '驳回';
                default:
                    return '无';
            }
        });
        $grid->column('after_status', __('处理状态'))->display(function () {
            switch ($this->after_status) {
                case 0:
                    return '未完成';
                case 1:
                    return '已完成';
                default:
                    return '无';
            }
        });
        $grid->column('logis_code', __('物流公司编码'));
        $grid->column('created_at', __('添加时间'));
        $grid->column('updated_at', __('修改时间'));
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(AfterSales::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('after_type', __('退货方式'));
//        $show->field('order_id', __('Order id'));
        $show->field('order_sn', __('订单号'));
        $show->field('goods_status', __('货物状态'))->as(function () {
            switch ($this->goods_status) {
                case 1:
                    return '已收货';
                case 2:
                    return '未收货';
                default:
                    return '无';
            }
        });
        $show->field('goods_num', __('商品数量'));
        $show->field('back_reason', __('退款原因'))->as(function () {
            $option = [
                1 => "不想要了",
                2 => "看到更便宜的商品",
                3 => "产品款式选错了",
                4 => "收货地址/手机号码填错了",
                5 => "有优惠券未使用",
                6 => "缺货",
                7 => "我要退款(无需退货)",
                8 => "已收到货(我要退货退款）",
                9 => "其他原因",

            ];
            return $option[$this->back_reason] ?? '';
        });
        $show->field('money', __('退款金额'));
        $show->field('province_id', __('省'))->unescape()->as(function () {
            $name = (new Area())->where('code', $this->address->province_id)->value('name');
            return $name;
        });
        $show->field('city_id', __('市'))->unescape()->as(function () {
            $name = (new Area())->where('code', $this->address->city_id)->value('name');
            return $name;
        });
        $show->field('district_id', __('县'))->unescape()->as(function () {
            $name = (new Area())->where('code', $this->address->district_id)->value('name');
            return $name;
        });
        $show->field('address.detail', __('详细地址'));
        $show->field('address.name', __('收件人'));
        $show->field('address.phone', __('联系电话'));

        $show->field('images', __('上传凭证'))->as(function () {
            return $this->images;
        })->image(Url::getHttpHost() . 'upload/', 100, 100);
        $show->field('describe', __('补充描述'));
        $show->field('logis_company', __('物流公司'));
        $show->field('logis_num', __('物流单号'));
        $show->field('status', __('审核状态'))->as(function () {
            switch ($this->status) {
                case 1:
                    return '审核通过';
                case 2:
                    return '驳回';
                default:
                    return '无';
            }
        });
        $show->field('after_status', __('售后状态'))->as(function () {
            switch ($this->after_status) {
                case 0:
                    return '未完成';
                case 1:
                    return '已完成';
                default:
                    return '无';
            }
        });
        $show->field('logis_code', __('物流公司编码'));
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new AfterSales());
        $form->radio('after_type', __('退货方式'))->options(['1' => '仅退款', '2' => '退货退款', '3' => '换货'])->default('1');
//        $form->number('order_id', __('Order id'));
        $form->text('order_sn', __('订单号'));
        $form->radio('goods_status', __('货物状态'))->options(['1' => '已收货', '2' => '未收货'])->default('2');
        $form->number('goods_num', __('商品数量'));
        $form->textarea('back_reason', __('退款原因'));
        $form->decimal('money', __('退款金额'));
//        $form->number('coupon_id', __('优惠券'));
//        $form->number('address_id', __('寄件地址'))->hidden();
        $form->multipleFile('images', __('上传凭证'))->uniqueName()->removable();
        $form->text('describe', __('补充描述'));
        $form->text('logis_company', __('物流公司'));
        $form->text('logis_num', __('物流单号'));
        $status = [
            'on' => ['value' => 1, 'text' => '审核通过', 'color' => 'default'],
            'off' => ['value' => 2, 'text' => '驳回', 'color' => 'primary'],
        ];
        $form->switch('status', __('审核状态'))->states($status)->default(2);
        $afterStatus = [
            'on' => ['value' => 0, 'text' => '未完成', 'color' => 'default'],
            'off' => ['value' => 1, 'text' => '已完成', 'color' => 'primary'],
        ];
        $form->switch('after_status', __('处理状态'))->states($afterStatus)->default(0);
//        $form->password('password')->tool()->hide();
        $form->text('logis_code', __('物流公司编码'));
        $form->saving(function ($form) {
            $form->ignore(['address_id']);//忽略字段,提交忽略字段
        });

        return $form;
    }
}
